enum LaunchTaskType {
  dataProcessing,
  appLauncher,
}

abstract class LaunchTask {
  LaunchTaskType get type => LaunchTaskType.dataProcessing;
  Future<bool> initialize(LaunchContext context);
}

class LaunchContext {
  bool env;
  LaunchContext(this.env);
}

class AppLauch {
  // 任务队列
  List<LaunchTask> taskList = [];
  LaunchContext context;
  AppLauch(this.context);

  addTask(LaunchTask task) {
    taskList.add(task);
  }

  bool _isRun = false;
  int currentIndex = 0;
// 执行队列
  run() {
    if (!_isRun) {
      _isRun = true;
      currentIndex = 0;
      _runStart();
    }
  }

  _runStart() async {
    dynamic task;
    if (taskList.length > currentIndex) {
      task = taskList[currentIndex];
    }
    if (task != null) {
      var res = await task.initialize(context);
      if (res) {
        taskList.remove(task);
      } else {
        currentIndex++;
      }
      _runStart();
    } else {
      _isRun = false;
      currentIndex = 0;
    }
  }
}
